<?php

namespace app\sales\controller;
use app\admin\model\Order as Model;
use app\admin\common\Base;
use app\admin\common\Order;
use think\Db;
//销货订单
class Invso extends Base
{
    public function index()
    {
        $data = $this->request->param();
        return (new Order) -> order_index($data,0); //所属类型 0，销售订单；1，销售单；2，退货单;3,购货订单，4，购货单，5，购货退货单
    }


    public function add()
    {
       $data = $this->request->param();
       return (new Order) -> order_add($data,0);//订单类别;0,销售订单，1，销售单，2，销售退货单，3,购货订单，4，购货单，5，购货退货单
    }

    //订单详情
    public function edit()
    {
        $data = $this->request->param();
        return (new Order) -> order_edit($data,0);//订单类别;0,销售订单，1，销售单，2，销售退货单，3,购货订单，4，购货单，5，购货退货单
    }

     //删除栏目1-1
    public function del()
    {
        $data = $this->request->param();
        return (new Order) -> order_del($data);
    }
    
    // 添加销售单-更新
    public function save()
    {
        $status = 0;
        $editId=0;
        $msg = "操作失败";
        $data = $this->request->param();
        //halt($data);
        $post=$data;
        Db::startTrans();
       // halt($data);
        $model = new Model();
        if (empty($data['id'])) {//添加-订单直接添加审核
            $info = Db::name('order')->where('sn', $post['sn'])->find();
            if (!empty($info)) {
                $msg = "存在相同的单据号";
                // 回滚事务
                Db::rollback();
                return ['status' => $status, 'msg' => $msg];
            }
            if(isset($data['o_shenhe'])&&$data['o_shenhe']=1){//需要直接审核
                unset($data['o_shenhe']);
                $data['shenhe_time'] = time();
                $data['shenhe_user'] = $this->Userinfo['id'];
                $data['status']=1;
            }

            $data['addtime'] = time();
            $data['enterprise_id'] = $this->EnterpriseId;
            $data['updatetime'] = time();

            $data['dj_time'] = strtotime($data['dj_time']);
            $data['jh_time'] = strtotime($data['jh_time']);
            $data['add_user'] = $this->Userinfo['id'];
            $data['last_user'] = $this->Userinfo['id'];
            //$data['by_admin'] = $this->Userinfo['id'];
            $data['o_type'] = 0; //订单类型
            $data['user_category'] = 0; //0客户，1经销商
            $data['big_type'] = 0; //0销货，1购货

            $invpoGoods = $data['movegood'];
            unset($data['movegood']);
            $result = $model->allowField(true)->save($data);
            //halt($result);
            if ($result) {
                if (add_order_good($model->id, $invpoGoods, $this->EnterpriseId, 0)) {
                    Db::commit(); // 提交事务  
                    $status = 1;
                    $editId = $model->id;
                    $msg = "添加成功";
                }
            }
            if($status!=1){
                Db::rollback();
            }
            return ['status' => $status, 'msg' => $msg, 'editId' => $editId];
        } else {//修改
            if (isset($data['sn'])) {
                   $info = Db::name('order')->where('sn', $post['sn'])->where('id','neq',$data['id'])->find();
                    if (!empty($info)) {
                        $msg = "存在相同的单据号";
                        // 回滚事务
                        Db::rollback();
                        return ['status' => $status, 'msg' => $msg];
                    }
            }
        
            if(isset($data['edit'])&&$data['edit']==1){//编辑保存
                $result = $this->edit_order($data);
                if ($result) {
                    Db::commit(); // 提交事务  
                    $status = 1;
                    $msg = "更新成功";
                }
                if($status!=1){
                    Db::rollback();
                }
                return json(['status' => $status, 'msg' => $msg]);
            }
        
        }
        return json(['status' => $status, 'msg' => $msg]);
    }

   
   //更新订单-审核与反审核,关闭订单与启用订单
    public function update()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        //halt($data);
        $post=$data;
        Db::startTrans();
        $model = new Model();
        if(isset($data['shenhe'])&&$data['shenhe']==1){////审核与反审核
            $ids = $data['ids'];
            $data['updatetime'] = time();
            $data['shenhe_time'] = time();
            $data['shenhe_user'] = $this->Userinfo['id'];
            $data['last_user'] = $this->Userinfo['id'];  
            $result = $model->allowField(true)->save($data, ["id" =>['in',$ids]]);
            if ($result) {
                Db::commit(); // 提交事务  
                $status = 1;
                $msg = "更新成功";
            }
            if($status!=1){
                Db::rollback();
            }
            return json(['status' => $status, 'msg' => $msg]);
        }
        if(isset($data['open'])&&$data['open']==1){//关闭订单与启用订单
            $ids = $data['ids'];
            $data['updatetime'] = time();
            //$data['shenhe_time'] = time();
            //$data['shenhe_user'] = $this->Userinfo['id'];
            $data['last_user'] = $this->Userinfo['id'];  
            $result = $model->allowField(true)->save($data, ["id" =>['in',$ids]]);
            if ($result) {
                Db::commit(); // 提交事务  
                $status = 1;
                $msg = "更新成功";
            }
            if($status!=1){
                Db::rollback();
            }
            return json(['status' => $status, 'msg' => $msg]);
        }
        if(isset($data['lock'])&&$data['lock']==1){//关闭订单与启用订单
            $ids = $data['ids'];
            $data['updatetime'] = time();
            $data['last_user'] = $this->Userinfo['id'];  
            $result = $model->allowField(true)->save($data, ["id" =>['in',$ids]]);
            if ($result) {
                Db::commit(); // 提交事务  
                $status = 1;
                $msg = "更新成功";
            }
            if($status!=1){
                Db::rollback();
            }
            return json(['status' => $status, 'msg' => $msg]);
        }
        return json(['status' => $status, 'msg' => $msg]);
    }

}
